import Path from "path";
import { readFileToFontData } from "../../src/lib/fonts/fontData";

test("Should be able to read VBR font data", async () => {
  const filename = Path.join(__dirname, "../data/fonts/font-vwf.png");
  const data = await readFileToFontData(filename);
  // Should have 96 characters in set
  expect(data.widths.length).toBe(96);
  // Should include additional 32 ASCII control characters
  expect(data.table.length).toBe(96 + 32);
  // Should determine that font was variable width
  expect(data.isVariableWidth).toBe(true);
  // Should determine that font was 1-bit
  expect(data.is1Bit).toBe(true);
});

test("Should map space character to ASCII control codes", async () => {
  const filename = Path.join(__dirname, "../data/fonts/font-vwf.png");
  const data = await readFileToFontData(filename);
  expect(data.table.slice(0, 32)).toEqual(Array.from(Array(32)).fill(0));
});

test("Should create correct font data", async () => {
  const filename = Path.join(__dirname, "../data/fonts/font-vwf.png");
  const data = await readFileToFontData(filename);
  // prettier-ignore
  const expectedData = [
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x00,0x00,
    0x00,0x00,0xA0,0xA0,0xA0,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x50,0x50,0xF8,0xF8,0x50,0x50,0xF8,0xF8,0x50,0x50,0x00,0x00,0x00,0x00,
    0x00,0x00,0x20,0x20,0x70,0x70,0xA0,0xA0,0x60,0x60,0x50,0x50,0xE0,0xE0,0x40,0x40,
    0x00,0x00,0xD0,0xD0,0xD0,0xD0,0x20,0x20,0x60,0x60,0x40,0x40,0xB0,0xB0,0xB0,0xB0,
    0x00,0x00,0x40,0x40,0xA0,0xA0,0x40,0x40,0xA8,0xA8,0x90,0x90,0x68,0x68,0x00,0x00,
    0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x20,0x20,0x40,0x40,0x80,0x80,0x80,0x80,0x80,0x80,0x40,0x40,0x20,0x20,
    0x00,0x00,0x80,0x80,0x40,0x40,0x20,0x20,0x20,0x20,0x20,0x20,0x40,0x40,0x80,0x80,
    0x00,0x00,0x20,0x20,0xA8,0xA8,0x70,0x70,0xA8,0xA8,0x20,0x20,0x00,0x00,0x00,0x00,
    0x00,0x00,0x20,0x20,0x20,0x20,0xF8,0xF8,0x20,0x20,0x20,0x20,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,
    0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,
    0x00,0x00,0x10,0x10,0x10,0x10,0x20,0x20,0x60,0x60,0x40,0x40,0x80,0x80,0x80,0x80,
    0x00,0x00,0x60,0x60,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x60,0x60,0x00,0x00,
    0x00,0x00,0x40,0x40,0xC0,0xC0,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x00,
    0x00,0x00,0x60,0x60,0x90,0x90,0x10,0x10,0x60,0x60,0x80,0x80,0xF0,0xF0,0x00,0x00,
    0x00,0x00,0x60,0x60,0x10,0x10,0x60,0x60,0x10,0x10,0x90,0x90,0x60,0x60,0x00,0x00,
    0x00,0x00,0x30,0x30,0x50,0x50,0x90,0x90,0xF0,0xF0,0x10,0x10,0x10,0x10,0x00,0x00,
    0x00,0x00,0x70,0x70,0x80,0x80,0xE0,0xE0,0x10,0x10,0x90,0x90,0x60,0x60,0x00,0x00,
    0x00,0x00,0x60,0x60,0x80,0x80,0xE0,0xE0,0x90,0x90,0x90,0x90,0x60,0x60,0x00,0x00,
    0x00,0x00,0xF0,0xF0,0x10,0x10,0x20,0x20,0x20,0x20,0x40,0x40,0x40,0x40,0x00,0x00,
    0x00,0x00,0x60,0x60,0x90,0x90,0x60,0x60,0x90,0x90,0x90,0x90,0x60,0x60,0x00,0x00,
    0x00,0x00,0x60,0x60,0x90,0x90,0x90,0x90,0x70,0x70,0x10,0x10,0x60,0x60,0x00,0x00,
    0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,
    0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,
    0x00,0x00,0x00,0x00,0x10,0x10,0x60,0x60,0x80,0x80,0x60,0x60,0x10,0x10,0x00,0x00,
    0x00,0x00,0x00,0x00,0xF8,0xF8,0x00,0x00,0x00,0x00,0xF8,0xF8,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x80,0x80,0x60,0x60,0x10,0x10,0x60,0x60,0x80,0x80,0x00,0x00,
    0x00,0x00,0x60,0x60,0x90,0x90,0x10,0x10,0x20,0x20,0x00,0x00,0x20,0x20,0x00,0x00,
    0x00,0x00,0x38,0x38,0x44,0x44,0x9A,0x9A,0xAA,0xAA,0xAA,0xAA,0x9E,0x9E,0x40,0x40,
    0x00,0x00,0x20,0x20,0x50,0x50,0x50,0x50,0xF8,0xF8,0x88,0x88,0x88,0x88,0x00,0x00,
    0x00,0x00,0xF0,0xF0,0x88,0x88,0xF0,0xF0,0x88,0x88,0x88,0x88,0xF0,0xF0,0x00,0x00,
    0x00,0x00,0x70,0x70,0x88,0x88,0x80,0x80,0x80,0x80,0x88,0x88,0x70,0x70,0x00,0x00,
    0x00,0x00,0xF0,0xF0,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0xF0,0xF0,0x00,0x00,
    0x00,0x00,0xF8,0xF8,0x80,0x80,0xF0,0xF0,0x80,0x80,0x80,0x80,0xF8,0xF8,0x00,0x00,
    0x00,0x00,0xF8,0xF8,0x80,0x80,0xF0,0xF0,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,
    0x00,0x00,0x70,0x70,0x88,0x88,0x80,0x80,0x98,0x98,0x88,0x88,0x78,0x78,0x00,0x00,
    0x00,0x00,0x88,0x88,0x88,0x88,0xF8,0xF8,0x88,0x88,0x88,0x88,0x88,0x88,0x00,0x00,
    0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,
    0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x80,0x80,
    0x00,0x00,0x88,0x88,0x90,0x90,0xA0,0xA0,0xE0,0xE0,0x90,0x90,0x88,0x88,0x00,0x00,
    0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xF8,0xF8,0x00,0x00,
    0x00,0x00,0x84,0x84,0xCC,0xCC,0xCC,0xCC,0xB4,0xB4,0xB4,0xB4,0x84,0x84,0x00,0x00,
    0x00,0x00,0x88,0x88,0xC8,0xC8,0xA8,0xA8,0xA8,0xA8,0x98,0x98,0x88,0x88,0x00,0x00,
    0x00,0x00,0x70,0x70,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x70,0x70,0x00,0x00,
    0x00,0x00,0xF0,0xF0,0x88,0x88,0x88,0x88,0xF0,0xF0,0x80,0x80,0x80,0x80,0x00,0x00,
    0x00,0x00,0x70,0x70,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x70,0x70,0x08,0x08,
    0x00,0x00,0xF0,0xF0,0x88,0x88,0x88,0x88,0xF0,0xF0,0x90,0x90,0x88,0x88,0x00,0x00,
    0x00,0x00,0x70,0x70,0x88,0x88,0x60,0x60,0x10,0x10,0x88,0x88,0x70,0x70,0x00,0x00,
    0x00,0x00,0xF8,0xF8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x00,
    0x00,0x00,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x70,0x70,0x00,0x00,
    0x00,0x00,0x88,0x88,0x88,0x88,0x88,0x88,0x50,0x50,0x50,0x50,0x20,0x20,0x00,0x00,
    0x00,0x00,0x84,0x84,0xB4,0xB4,0xB4,0xB4,0xB4,0xB4,0x48,0x48,0x48,0x48,0x00,0x00,
    0x00,0x00,0x88,0x88,0x50,0x50,0x20,0x20,0x20,0x20,0x50,0x50,0x88,0x88,0x00,0x00,
    0x00,0x00,0x88,0x88,0x50,0x50,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x00,
    0x00,0x00,0xF8,0xF8,0x08,0x08,0x10,0x10,0x60,0x60,0x80,0x80,0xF8,0xF8,0x00,0x00,
    0x00,0x00,0xE0,0xE0,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xE0,0xE0,
    0x00,0x00,0x80,0x80,0x80,0x80,0x40,0x40,0x60,0x60,0x20,0x20,0x10,0x10,0x10,0x10,
    0x00,0x00,0xE0,0xE0,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xE0,0xE0,
    0x00,0x00,0x20,0x20,0x50,0x50,0x88,0x88,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0xF8,
    0x00,0x00,0x80,0x80,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x60,0x60,0x10,0x10,0x70,0x70,0x90,0x90,0x70,0x70,0x00,0x00,
    0x00,0x00,0x80,0x80,0xE0,0xE0,0x90,0x90,0x90,0x90,0x90,0x90,0xE0,0xE0,0x00,0x00,
    0x00,0x00,0x00,0x00,0x60,0x60,0x90,0x90,0x80,0x80,0x90,0x90,0x60,0x60,0x00,0x00,
    0x00,0x00,0x10,0x10,0x70,0x70,0x90,0x90,0x90,0x90,0x90,0x90,0x70,0x70,0x00,0x00,
    0x00,0x00,0x00,0x00,0x60,0x60,0x90,0x90,0xF0,0xF0,0x80,0x80,0x70,0x70,0x00,0x00,
    0x00,0x00,0x30,0x30,0x40,0x40,0xE0,0xE0,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x00,
    0x00,0x00,0x00,0x00,0x70,0x70,0x90,0x90,0x90,0x90,0x70,0x70,0x10,0x10,0x60,0x60,
    0x00,0x00,0x80,0x80,0xE0,0xE0,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x00,0x00,
    0x00,0x00,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,
    0x00,0x00,0x40,0x40,0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x80,0x80,
    0x00,0x00,0x80,0x80,0x90,0x90,0xA0,0xA0,0xC0,0xC0,0xA0,0xA0,0x90,0x90,0x00,0x00,
    0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x40,0x40,0x00,0x00,
    0x00,0x00,0x00,0x00,0xD0,0xD0,0xA8,0xA8,0xA8,0xA8,0xA8,0xA8,0xA8,0xA8,0x00,0x00,
    0x00,0x00,0x00,0x00,0xE0,0xE0,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x00,0x00,
    0x00,0x00,0x00,0x00,0x60,0x60,0x90,0x90,0x90,0x90,0x90,0x90,0x60,0x60,0x00,0x00,
    0x00,0x00,0x00,0x00,0xE0,0xE0,0x90,0x90,0x90,0x90,0xE0,0xE0,0x80,0x80,0x80,0x80,
    0x00,0x00,0x00,0x00,0x70,0x70,0x90,0x90,0x90,0x90,0x70,0x70,0x10,0x10,0x10,0x10,
    0x00,0x00,0x00,0x00,0xE0,0xE0,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,
    0x00,0x00,0x00,0x00,0x70,0x70,0x80,0x80,0x60,0x60,0x10,0x10,0xE0,0xE0,0x00,0x00,
    0x00,0x00,0x40,0x40,0xE0,0xE0,0x40,0x40,0x40,0x40,0x40,0x40,0x20,0x20,0x00,0x00,
    0x00,0x00,0x00,0x00,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x70,0x70,0x00,0x00,
    0x00,0x00,0x00,0x00,0x90,0x90,0x90,0x90,0x90,0x90,0x60,0x60,0x60,0x60,0x00,0x00,
    0x00,0x00,0x00,0x00,0x88,0x88,0xA8,0xA8,0xA8,0xA8,0x50,0x50,0x50,0x50,0x00,0x00,
    0x00,0x00,0x00,0x00,0x90,0x90,0x90,0x90,0x60,0x60,0x90,0x90,0x90,0x90,0x00,0x00,
    0x00,0x00,0x00,0x00,0x90,0x90,0x90,0x90,0x60,0x60,0x60,0x60,0x40,0x40,0x80,0x80,
    0x00,0x00,0x00,0x00,0xF0,0xF0,0x10,0x10,0x60,0x60,0x80,0x80,0xF0,0xF0,0x00,0x00,
    0x00,0x00,0x30,0x30,0x40,0x40,0x40,0x40,0x80,0x80,0x40,0x40,0x40,0x40,0x30,0x30,
    0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
    0x00,0x00,0xC0,0xC0,0x20,0x20,0x20,0x20,0x10,0x10,0x20,0x20,0x20,0x20,0xC0,0xC0,
    0x00,0x00,0x50,0x50,0xA0,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x20,0x20,0x50,0x50,0x88,0x88,0x88,0x88,0xF8,0xF8,0x00,0x00
  ]
  for (let i = 0; i < data.data.length; i++) {
    expect(data.data[i]).toEqual(expectedData[i]);
  }
});

test("Should be able to read monospace font data", async () => {
  const filename = Path.join(__dirname, "../data/fonts/font-mono.png");
  const data = await readFileToFontData(filename);
  // Should have 203 characters in set
  expect(data.widths.length).toBe(203);
  // Should include additional 32 ASCII control characters
  expect(data.table.length).toBe(224 + 32);
  // Should determine that font was variable width
  expect(data.isVariableWidth).toBe(false);
  // Should determine that font was 1-bit
  expect(data.is1Bit).toBe(true);
});
